package com.gionee.navil.lockscreen;

import android.content.ContentResolver;
import android.content.Context;
import android.provider.Settings;

public class GnLockScreenUtils {
	private final String GN_LOCK_SCREEN_APPWIDGET_IDS = "gn_navil_widget_ids";
	private static final String TAG = "GnLockScreenUtils";

	private ContentResolver mContentResolver;

	public GnLockScreenUtils(Context context) {
		mContentResolver = context.getContentResolver();
	}

	public boolean addAppWidget(int widgetId, int index) {
		int[] widgets = getAppWidgets();
		if (widgets == null) {
			return false;
		}
		if (index < 0 || index > widgets.length) {
			return false;
		}
		int[] newWidgets = new int[widgets.length + 1];
		for (int i = 0, j = 0; i < newWidgets.length; i++) {
			if (index == i) {
				newWidgets[i] = widgetId;
				i++;
			}
			if (i < newWidgets.length) {
				newWidgets[i] = widgets[j];
				j++;
			}
		}
		writeAppWidgets(newWidgets);
		return true;
	}

	public int[] getAppWidgets() {
		String appWidgetIdString = Settings.Secure.getString(mContentResolver,
				GN_LOCK_SCREEN_APPWIDGET_IDS);
		GnLog.d(TAG, "appWidgetIdString=" + appWidgetIdString);
		String delims = ",";
		if (appWidgetIdString != null && appWidgetIdString.length() > 0) {
			String[] appWidgetStringIds = appWidgetIdString.split(delims);
			int[] appWidgetIds = new int[appWidgetStringIds.length];
			for (int i = 0; i < appWidgetStringIds.length; i++) {
				String appWidget = appWidgetStringIds[i];
				try {
					appWidgetIds[i] = Integer.decode(appWidget);
				} catch (NumberFormatException e) {
					GnLog.d(TAG, "Error when parsing widget id " + appWidget);
					return null;
				}
			}
			return appWidgetIds;
		}
		return new int[0];
	}

	public boolean removeAppWidget(int widgetId) {
		int[] widgets = getAppWidgets();

		if (widgets.length == 0) {
			return false;
		}

		int[] newWidgets = new int[widgets.length - 1];
		for (int i = 0, j = 0; i < widgets.length; i++) {
			if (widgets[i] == widgetId) {
				// continue...
			} else if (j >= newWidgets.length) {
				// we couldn't find the widget
				return false;
			} else {
				newWidgets[j] = widgets[i];
				j++;
			}
		}
		writeAppWidgets(newWidgets);
		return true;
	}

	private void writeAppWidgets(int[] appWidgetIds) {
		Settings.Secure
				.putString(mContentResolver, GN_LOCK_SCREEN_APPWIDGET_IDS,
						combineStrings(appWidgetIds, ","));
	}

	private static String combineStrings(int[] list, String separator) {
		int listLength = list.length;

		switch (listLength) {
		case 0: {
			return "";
		}
		case 1: {
			return Integer.toString(list[0]);
		}
		}

		int strLength = 0;
		int separatorLength = separator.length();

		String[] stringList = new String[list.length];
		for (int i = 0; i < listLength; i++) {
			stringList[i] = Integer.toString(list[i]);
			strLength += stringList[i].length();
			if (i < listLength - 1) {
				strLength += separatorLength;
			}
		}

		StringBuilder sb = new StringBuilder(strLength);

		for (int i = 0; i < listLength; i++) {
			sb.append(list[i]);
			if (i < listLength - 1) {
				sb.append(separator);
			}
		}

		return sb.toString();
	}

}
